<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use DB;

class Cat extends Model
{
    protected $table = 'cats';
    protected $primaryKey = 'cat_id';
    public $timestamps = false;


    public function getTree($pid = 0,$lev = 0){

        $tree = array();
        $cats = DB::table('cats')->get();
        foreach($cats as $cat){
            if($cat->parent_id == $pid){
                $cat->lev = $lev;
                $tree[] = $cat;

                $tree = array_merge($tree,$this->getTree($cat->cat_id,$lev+1));

            }
        }
        return $tree;
    }
}
